package com.xzy.web.controller;

import com.xzy.custom.AjaxResult;
import com.xzy.custom.Page;
import com.xzy.custom.ResultMap;
import com.xzy.entities.SysRole;
import com.xzy.service.SysRoleService;
import com.xzy.web.form.SysRoleForm;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * TODO
 *
 * @author XiangZhongyi
 * ClassName: 权限管理
 * date: 2021/9/20 20:05
 */
@Controller
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping("/userType")
    public String userType(){
        return "admin/user/usertype/manage";
    }

    @RequestMapping("/findAllUserType")
    @ResponseBody
    public ResultMap<List<SysRole>> findAllUserType(Page page, @RequestParam("limit")int limit,@Param("name") String name){
        page.setRows(limit);
        page.setPage((page.getPage()-1)*limit);
        Integer total = sysRoleService.findAllUserTypeCount(page,name);
        List<SysRole> list = sysRoleService.findAllUserType(page,name);
        System.out.println(list);
        return new ResultMap<List<SysRole>>("",list,0,total);
    }

    @RequestMapping("/userTypeEdit")
    @ResponseBody
    public String userTypeEdit(){
        return "typeEdit";
    }

    @RequestMapping("/typeEdit")
    public String typeEdit(SysRoleForm form, ModelMap map){
        SysRole model = new SysRole();
        Integer id = form.getId();
        System.out.println(id);
        if (id != null){
            model = sysRoleService.findById(id);
        }
        map.put("model",model);
        return "admin/user/userType/edit";
    }

    @RequestMapping("/saveType")
    @ResponseBody
    public Object saveType(SysRoleForm form){
        try {
            if ("".equals(form.getRoleName())){
                return new AjaxResult(false,"请填写类型名称！");
            }else if("".equals(form.getRoleCode())){
                return new AjaxResult(false,"请填写类型代码！");
            }else{
                if (form.getId() != null){
                    sysRoleService.updateById(form);
                }else {
                    sysRoleService.insertType(form);
                }
            }
            return new AjaxResult("数据保存成功！");
        }catch (Exception e){
            return new AjaxResult(false,"数据保存失败！");
        }
    }

    @RequestMapping("/delUserType")
    @ResponseBody
    public Object delBookType(String ids){
        try {
            String[] split = ids.split(",");
            for (int i = 0; i < split.length;i++){
                sysRoleService.deleteById(Integer.parseInt(split[i]));
            }
            return new AjaxResult("删除成功！");
        }catch (Exception e){
            return new AjaxResult(false, "数据删除失败！");
        }
    }
}
